package cn.neusoft.luofa603.yuejian_29.fragment;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.neusoft.luofa603.yuejian_29.R;
import cn.neusoft.luofa603.yuejian_29.adapter.UnionAdapter;

import static android.R.id.tabs;

public class UnionFragment extends Fragment {
    private View view;
    private TabLayout tabs;
    private ViewPager viewPager;
    private RollPagerView rollPV;
    private List<String> mTitle = new ArrayList<String>();
    private List<Fragment> mFragment = new ArrayList<Fragment>();

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
         view = inflater.inflate(R.layout.union_fragment, container, false);
        rollPV = (RollPagerView) view.findViewById(R.id.rollUV);
        //设置每个图片的切换时间
        rollPV.setPlayDelay(3000);
        //设置图片切换动画时间
        rollPV.setAnimationDurtion(500);
        //设置指示器:
        rollPV.setAdapter(new RollPagerAdapter());

        initViews();
        initData();
        initEvents();
        UnionAdapter unionAdapter = new UnionAdapter(getChildFragmentManager(),mTitle,mFragment);
        viewPager.setAdapter(unionAdapter);
        tabs.setupWithViewPager(viewPager);
        return view;

    }

    public void initViews(){

        tabs = (TabLayout) view.findViewById(R.id.Ty);
        viewPager = (ViewPager) view.findViewById(R.id.VP);


        mTitle.add("精选活动");
        mTitle.add("权威大师");
        mTitle.add("最近比赛");
        mFragment.add(new ActivityFragment());
        mFragment.add(new MasterFragment());
        mFragment.add(new CompetFragment());

    }
    public void initData(){

    }
    public void initEvents(){

    }

    //适配器
    private class RollPagerAdapter extends StaticPagerAdapter {

        private int images[] = {
                R.drawable.img5,
                R.drawable.img6,
                R.drawable.img7,
                R.drawable.img8
        };

        @Override
        public View getView(ViewGroup container, int position) {

            ImageView view=new ImageView(container.getContext());
            //设置图片资源
            view.setImageResource(images[position]);
            //设置高度和宽度
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置拉伸方式
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);

            return view;
        }

        @Override
        public int getCount() {
            return images.length;
        }
    }
}
